package com.ruoyi.meeting.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.meeting.domain.TmdMeetingManager;
import com.ruoyi.meeting.dto.TmdMeetingDto;
import com.ruoyi.meeting.dto.TmdMeetingPersonDto;
import com.ruoyi.meeting.mapper.TmdMeetingManagerMapper;
import com.ruoyi.meeting.vo.MeetingRecordVo;
import com.ruoyi.meeting.vo.TmdMeetingApplicationVo;
import com.ruoyi.system.domain.SysRoleDept;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.meeting.mapper.TmdMeetingApplicationMapper;
import com.ruoyi.meeting.domain.TmdMeetingApplication;
import com.ruoyi.meeting.service.ITmdMeetingApplicationService;
import com.ruoyi.common.core.text.Convert;

/**
 * 会议列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class TmdMeetingApplicationServiceImpl implements ITmdMeetingApplicationService {
    @Autowired
    private TmdMeetingApplicationMapper tmdMeetingApplicationMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private TmdMeetingManagerMapper tmdMeetingManagerMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询会议列表
     *
     * @param id 会议列表主键
     * @return 会议列表
     */
    @Override
    public TmdMeetingApplication selectTmdMeetingApplicationById(Long id) {
        return tmdMeetingApplicationMapper.selectTmdMeetingApplicationById(id);
    }

    @Override
    public MeetingRecordVo selectTmdMeetingApplicationVoById(Long id) {
        // 查询会议记录
        TmdMeetingApplication tmdMeetingApplication = tmdMeetingApplicationMapper.selectTmdMeetingApplicationById(id);
        // 查询此次会议的与会人和分享人
        List<TmdMeetingPersonDto> tmdMeetingPersonDtos = tmdMeetingApplicationMapper.selectJoinAndShareStaff(id);

        MeetingRecordVo meetingRecordVo = new MeetingRecordVo();
        meetingRecordVo.setAddressName(tmdMeetingManagerMapper.selectTmdMeetingManagerById(tmdMeetingApplication.getAddressId()).getMeetingAddressName());
        SysDept sysDept = sysDeptMapper.selectDeptById(tmdMeetingApplication.getDeptId());
        SysUser hostUser = sysUserMapper.selectUserById(tmdMeetingApplication.getHostId());
        SysUser linkManUser = sysUserMapper.selectUserById(tmdMeetingApplication.getLinkmanId());
        meetingRecordVo.setDeptName(sysDept.getDeptName());
        meetingRecordVo.setId(tmdMeetingApplication.getId());
        meetingRecordVo.setMeetingName(tmdMeetingApplication.getMeetingName());
        meetingRecordVo.setAddressId(tmdMeetingApplication.getAddressId());
        meetingRecordVo.setDeptId(tmdMeetingApplication.getDeptId());
        meetingRecordVo.setCreatePerson(tmdMeetingApplication.getCreatePerson());
        meetingRecordVo.setCreateTime(tmdMeetingApplication.getCreateTime());
        meetingRecordVo.setStatus(tmdMeetingApplication.getStatus());
        meetingRecordVo.setLevel(tmdMeetingApplication.getLevel());
        meetingRecordVo.setHostId(tmdMeetingApplication.getHostId());
        meetingRecordVo.setLinkmanId(tmdMeetingApplication.getLinkmanId());
        meetingRecordVo.setStartTime(tmdMeetingApplication.getStartTime());
        meetingRecordVo.setEndTime(tmdMeetingApplication.getEndTime());
        meetingRecordVo.setType(tmdMeetingApplication.getType());
        meetingRecordVo.setFile(tmdMeetingApplication.getFile());
        meetingRecordVo.setContext(tmdMeetingApplication.getContext());
        meetingRecordVo.setNotes(tmdMeetingApplication.getNotes());
        meetingRecordVo.setMeetingRecord(tmdMeetingApplication.getMeetingRecord());
        meetingRecordVo.setHostName(hostUser.getUserName());
        meetingRecordVo.setLinkmanName(linkManUser.getUserName());
//      设置与会人与分享人
        meetingRecordVo.setMeetingPersonList(tmdMeetingPersonDtos);
        return meetingRecordVo;
    }

    /**
     * 查询会议列表列表
     *
     * @param tmdMeetingApplication 会议列表
     * @return 会议列表
     */
    @Override
    public List<TmdMeetingApplication> selectTmdMeetingApplicationList(TmdMeetingApplication tmdMeetingApplication) {
        return tmdMeetingApplicationMapper.selectTmdMeetingApplicationList(tmdMeetingApplication);
    }

    @Override
    public List<MeetingRecordVo> selectMeetingRecordList(TmdMeetingApplication tmdMeetingApplication) {
        List<TmdMeetingApplication> tmdMeetingApplications = tmdMeetingApplicationMapper.selectTmdMeetingApplicationList(tmdMeetingApplication);
        List<MeetingRecordVo> meetingRecordVos = new ArrayList<>();
        for (TmdMeetingApplication eve:tmdMeetingApplications){
            MeetingRecordVo meetingRecordVo= new MeetingRecordVo();
            meetingRecordVo.setAddressName(tmdMeetingManagerMapper.selectTmdMeetingManagerById(eve.getAddressId()).getMeetingAddressName());
            SysDept sysDept = sysDeptMapper.selectDeptById(eve.getDeptId());
            meetingRecordVo.setDeptName(sysDept.getDeptName());
            meetingRecordVo.setId(eve.getId());
            meetingRecordVo.setMeetingName(eve.getMeetingName());
            meetingRecordVo.setAddressId(eve.getAddressId());
            meetingRecordVo.setDeptId(eve.getDeptId());
            meetingRecordVo.setCreatePerson(eve.getCreatePerson());
            meetingRecordVo.setCreateTime(eve.getCreateTime());
            meetingRecordVo.setStatus(eve.getStatus());
            meetingRecordVo.setLevel(eve.getLevel());
            meetingRecordVo.setHostId(eve.getHostId());
            meetingRecordVo.setLinkmanId(eve.getLinkmanId());
            meetingRecordVo.setStartTime(eve.getStartTime());
            meetingRecordVo.setEndTime(eve.getEndTime());
            meetingRecordVo.setType(eve.getType());
            meetingRecordVo.setFile(eve.getFile());
            meetingRecordVo.setContext(eve.getContext());
            meetingRecordVo.setNotes(eve.getNotes());
            meetingRecordVo.setMeetingRecord(eve.getMeetingRecord());


            meetingRecordVo.setDeptId(eve.getDeptId());


            meetingRecordVos.add(meetingRecordVo);
        }
        return meetingRecordVos;
    }

    /**
     * 新增会议列表
     *
     * @param  tmdMeetingDto
     * @return 结果
     */
    @Override
    //传过来一个dto对象，里面包含了会议信息 插入一个新的会议
    public int insertTmdMeetingApplication(TmdMeetingDto tmdMeetingDto) {
        TmdMeetingApplication tmdMeetingApplication = new TmdMeetingApplication();
        //tmdMeetingApplication.setId(tmdMeetingDto.getId());
        tmdMeetingApplication.setHostId(tmdMeetingDto.getHostId());
        tmdMeetingApplication.setMeetingName(tmdMeetingDto.getMeetingName());
        tmdMeetingApplication.setAddressId(tmdMeetingDto.getAddressId());
        tmdMeetingApplication.setDeptId(tmdMeetingDto.getDeptId());
        tmdMeetingApplication.setStartTime(tmdMeetingDto.getStartTime());
        tmdMeetingApplication.setEndTime(tmdMeetingDto.getEndTime());
        tmdMeetingApplication.setType(tmdMeetingDto.getType());
        tmdMeetingApplication.setLinkmanId(tmdMeetingDto.getLinkmanId());
        tmdMeetingApplication.setCreatePerson(tmdMeetingDto.getCreatePerson());
        tmdMeetingApplication.setStatus(tmdMeetingDto.getStatus());
        tmdMeetingApplication.setLevel(tmdMeetingDto.getLevel());
        tmdMeetingApplication.setFile(tmdMeetingDto.getFile());
        tmdMeetingApplication.setContext(tmdMeetingDto.getContext());
        tmdMeetingApplication.setNotes(tmdMeetingDto.getNotes());
        tmdMeetingApplication.setCreateTime(DateUtils.getNowDate());
        //插入基本信息
        int i = tmdMeetingApplicationMapper.insertTmdMeetingApplication(tmdMeetingApplication);
        //获得前端传递的与会人和分享人信息 数组中存放的是personid 和 类型id 和会议id

        Integer meetingId = Math.toIntExact(tmdMeetingApplication.getId());
        Integer[] sharedPerson = tmdMeetingDto.getSharedPerson();
        Integer[] meetingPerson = tmdMeetingDto.getMeetingPerson();
        if (sharedPerson != null){
            for (Integer eve:sharedPerson){
                TmdMeetingPersonDto tmdMeetingPersonDto = new TmdMeetingPersonDto();
                tmdMeetingPersonDto.setMeetingId(meetingId);
                tmdMeetingPersonDto.setPersonId(eve);
                tmdMeetingPersonDto.setMeetingPersonType(1);
                tmdMeetingApplicationMapper.insertTmdMeetingPerson(tmdMeetingPersonDto);
            }
        }
        if (meetingPerson != null){
            for (Integer eve:meetingPerson){
                TmdMeetingPersonDto tmdMeetingPersonDto = new TmdMeetingPersonDto();
                tmdMeetingPersonDto.setMeetingId(meetingId);
                tmdMeetingPersonDto.setPersonId(eve);
                tmdMeetingPersonDto.setMeetingPersonType(0);
                tmdMeetingApplicationMapper.insertTmdMeetingPerson(tmdMeetingPersonDto);
            }
        }

        return i;
    }

    /**
     * 修改会议列表
     *
     * @param tmdMeetingApplication 会议列表
     * @return 结果
     */
    @Override
    public int updateTmdMeetingApplication(TmdMeetingApplication tmdMeetingApplication) {
        return tmdMeetingApplicationMapper.updateTmdMeetingApplication(tmdMeetingApplication);
    }

    /**
     * 批量删除会议列表
     *
     * @param ids 需要删除的会议列表主键
     * @return 结果
     */
    @Override
    public int deleteTmdMeetingApplicationByIds(String ids) {
        return tmdMeetingApplicationMapper.deleteTmdMeetingApplicationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会议列表信息
     *
     * @param id 会议列表主键
     * @return 结果
     */
    @Override
    public int deleteTmdMeetingApplicationById(Long id) {
        return tmdMeetingApplicationMapper.deleteTmdMeetingApplicationById(id);
    }

    @Override
    public List<TmdMeetingManager> selectAddList() {
        return tmdMeetingApplicationMapper.selectAddList();
    }

    @Override
    public List<SysUser> selectUserList() {
        return tmdMeetingApplicationMapper.selectUserList();
    }

    @Override
    public SysUser getUserPhoneNumber(Integer userId) {
        return tmdMeetingApplicationMapper.getUserPhoneNumber(userId);
    }

    @Override
    public List<SysUser> getDeptUserList(Integer deptId) {
        return tmdMeetingApplicationMapper.getDeptUserList(deptId);
    }

    @Override
    public Integer getLinkmanId(Long id) {
        return tmdMeetingApplicationMapper.getLinkmanId(id);
    }


    @Override
    public SysUser getUser(Integer userId) {
        return tmdMeetingApplicationMapper.getUser(userId);
    }

    @Override
    public List<TmdMeetingManager> getMeetingName() {
        return tmdMeetingApplicationMapper.getMeetingName();
    }

    @Override
    public void beginning(Long id) {
        tmdMeetingApplicationMapper.beginning(id);
    }

    @Override
    public void endless(Long id) {
        tmdMeetingApplicationMapper.endless(id);
    }


    @Override
    public int getAddressIdById(Long id) {
        return tmdMeetingApplicationMapper.getAddressIdById(id);
    }

    @Override
    public void beginningRoom(int addressIdById) {

        tmdMeetingApplicationMapper.beginningRoom(addressIdById);
    }

    @Override
    public TmdMeetingManager getRoom(Long id) {
        return tmdMeetingApplicationMapper.getRoom(id);
    }

    @Override
    public void endRoom(Long addressId) {
        tmdMeetingApplicationMapper.endRoom(addressId);
    }

    @Override
    public TmdMeetingApplicationVo getMeetingRecord(Long id) {
        return tmdMeetingApplicationMapper.getMeetingRecord(id);
    }

    @Override
    public String getMeetingRoom(Long addressId) {
        return tmdMeetingApplicationMapper.getMeetingRoom(addressId);
    }

    @Override
    public List<TmdMeetingManager> getMeetingPlace() {
        return tmdMeetingApplicationMapper.getMeetingPlace();
    }

    @Override
    public Integer getHostId(Long id) {
        return tmdMeetingApplicationMapper.getHostId(id);
    }

}
